package com.jie.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jie.entity.SportFile;
import com.jie.mapper.SportFileMapper;
import com.jie.service.ISportFileService;
import com.jie.store.context.UploadStrategyContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 系统的文件存储表 服务实现类
 * </p>
 *
 * @author jie
 * @since 2024-05-29
 */
@Service
public class
SportFileServiceImpl extends ServiceImpl<SportFileMapper, SportFile> implements ISportFileService {

    @Autowired
    private UploadStrategyContext uploadStrategyContext;
    @Value("${upload.minio.endpoint}")
    private String endpoint;
    @Override
    public Integer uploadFile(MultipartFile file) {
        //获取文件信息
        String url = uploadStrategyContext.executeUploadStrategy(file);
        //保存文件信息到数据库
        SportFile sportFile = SportFile.builder()
                .fileSize(file.getSize())
                .fileUrl(url)
                .fileName(file.getName())
                .extendName(file.getContentType())
                .filePath(url.substring(endpoint.length()))
                .build();
        this.baseMapper.insert(sportFile);
        return sportFile.getId();
    }
}
